<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading class="article-loading" vertical v-if="loading">
      加载中...
    </van-loading>
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | date }}</p>
        </div>
        <van-button round size="small" type="info" @click="toggleFollow">{{
          article.is_followed ? '取关' : '+ 关注'
        }}</van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
          @click="toggleLike"
          >{{ article.attitude === 1 ? '取消点赞' : '点赞' }}</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          >不喜欢</van-button
        >
      </div>
    </div>
    <!-- /文章详情 -->

    <!-- 评论组件 -->
    <ArticleComment></ArticleComment>
  </div>
</template>

<script>
import ArticleComment from './comment'
import {
  getArticleDetail,
  followUser,
  unFollowUser,
  deleteLike,
  addLike
} from '@/api/article'
import { mapState } from 'vuex'
export default {
  name: 'ArticleIndex',
  computed: {
    ...mapState('user', ['tokenInfo'])
  },
  components: {
    ArticleComment
  },
  data() {
    return {
      // 控制加载状态
      loading: true,
      // 存放文章详情
      article: {}
    }
  },
  async created() {
    const res = await getArticleDetail(this.$route.params.id)
    this.article = res.data.data
    this.loading = false
  },
  methods: {
    /*
      1. 判断用户是否登录
      2. 如果用户没有登录，跳转到登录页面
      3. 如果用户登录了，判断用户是否关注
      4. 如果关注了，发送取消关注请求，如果没有关注，发送关注请求
      5. 重新渲染页面
    */
    checkLogin() {
      if (!this.tokenInfo.token) {
        this.$router.push({
          path: '/login',
          query: {
            // 希望登录成功的时候，不要push回来，back回来
            goBack: true
          }
        })
        this.$toast('请先登录')
        return false
      }
      return true
    },
    async toggleFollow() {
      if (!this.checkLogin()) return
      try {
        if (this.article.is_followed) {
          // 取关
          await unFollowUser(this.article.aut_id.toString())
          this.$toast.success('取关成功')
        } else {
          // 关注
          await followUser(this.article.aut_id.toString())
          this.$toast.success('关注成功')
        }
        this.article.is_followed = !this.article.is_followed
      } catch (err) {
        this.$toast.fail('关注失败')
      }
    },
    async toggleLike() {
      if (!this.checkLogin()) return
      try {
        if (this.article.attitude === 1) {
          // 取关
          await deleteLike(this.article.art_id.toString())
          this.$toast.success('取消点赞成功')
        } else {
          // 关注
          await addLike(this.article.art_id.toString())
          this.$toast.success('点赞成功')
        }
        this.article.attitude = this.article.attitude === 1 ? -1 : 1
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
